﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class Applique : MonoBehaviour
{
    public Mesh mesh;
    public Texture texture;
    public Vector2 size;
    public List<Vector3> points;
    public List<Vector3> vertices;
    public List<int> triangles;

    [HideInInspector]
    public float currentTime = 0;
    
    void Awake()
    {
        mesh = GetComponent<MeshFilter>().mesh;
        currentTime = Time.time;
    }
    
    void FindPoint()
    {
        Ray ray = new Ray();
        ray.direction = Vector3.down;
        ray.origin = new Vector3(transform.position.x, transform.position.y + 10f, transform.position.z);

        RaycastHit hit;
        if (Physics.Raycast(ray, out hit))
        {
            var point = hit.point;
            if (!points.Contains(point))
                points.Add(point);
        }
    }

    void CreateMesh()
    {
        vertices.Clear();
        triangles.Clear();
        for (int i = 0; i < points.Count; ++i)
        {
            var point = points[i];

            Vector3 p0 = point + new Vector3(size.x, 0, size.y) / 2f;
            Vector3 p1 = point + new Vector3(-size.x, 0, size.y) / 2f;
            Vector3 p2 = point + new Vector3(-size.x, 0, -size.y) / 2f;
            Vector3 p3 = point + new Vector3(size.x, 0, -size.y) / 2f;

            int[] cursor = new int[4];

            if (i == 0)
            {
                vertices.Add(p0);
                vertices.Add(p1);
                vertices.Add(p2);
                vertices.Add(p3);

                triangles.Add(0);
                triangles.Add(1);
                triangles.Add(2);
                
                triangles.Add(2);
                triangles.Add(3);
                triangles.Add(0);

                cursor[0] = 0;
                cursor[1] = 1;
                cursor[2] = 2;
                cursor[3] = 3;
            }
            else
            {
                var prevPoint = points[i - 1];
                var dir = (point - prevPoint).normalized;

                var x = dir.x;
                var z = dir.z;
                
                if (x == 0 && z > 0) //top
                {
                    vertices.Add(p0);
                    vertices.Add(p1);
                }
                else if (x == 0 && z < 0)//bottom
                {
                    vertices.Add(p2);
                    vertices.Add(p3);
                }
                else if (x > 0 && z == 0)//left
                {
                    vertices.Add(p3);
                    vertices.Add(p0);
                }
                else if (x < 0 && z == 0)//right
                {
                    vertices.Add(p1);
                    vertices.Add(p2);
                }
                else if (x > 0 && z > 0)
                {
                    if (x / z > 1)
                    {
                        vertices.Add(p3);
                        vertices.Add(p0);
                    }
                    else
                    {
                        vertices.Add(p0);
                        vertices.Add(p1);
                    }
                }
                else if (x > 0 && z < 0)
                {
                    if (x / (-z) > 1)
                    {
                        vertices.Add(p3);
                        vertices.Add(p0);
                    }
                    else
                    {
                        vertices.Add(p2);
                        vertices.Add(p3);
                    }
                }
                else if (x < 0 && z > 0)
                {
                    if ((-x) / z > 1)
                    {
                        vertices.Add(p1);
                        vertices.Add(p2);
                    }
                    else
                    {
                        vertices.Add(p0);
                        vertices.Add(p1);
                    }
                }
                else if (x < 0 && z < 0)
                {
                    if ((-x) / (-z) > 1)
                    {
                        vertices.Add(p3);
                        vertices.Add(p0);
                    }
                    else
                    {
                        vertices.Add(p2);
                        vertices.Add(p3);
                    }
                }
                
            }

        }

        mesh.vertices = vertices.ToArray();
        mesh.triangles = triangles.ToArray();
    }

    

    void Update()
    {
        if (Time.time - currentTime > 1f)
        {
            currentTime = Time.time;

            FindPoint();
            CreateMesh();
        }
    }
}
